/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.connection.listeners;

import java.util.ArrayList;
import java.util.Iterator;

import lombok.Getter;
import lombok.Setter;

import org.apache.log4j.Level;
import org.pircbotx.ReplyConstants;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.ServerResponseEvent;

import com.google.code.cubeirc.connection.CubeIRC;
import com.google.code.cubeirc.debug.DebuggerQueue;
import com.google.code.cubeirc.queue.MessageQueue;
import com.google.code.cubeirc.queue.MessageQueueEnum;

public class MOTDListener extends ListenerAdapter<CubeIRC>{
	@Getter @Setter
	private ArrayList<String> amotd = new ArrayList<String>();
	
	
	@Override
	public void onServerResponse(ServerResponseEvent<CubeIRC> event) throws Exception {
		
		if (event.getCode() == ReplyConstants.RPL_MOTDSTART)
			getAmotd().add("Message of the day");
		
		if (event.getCode() == ReplyConstants.RPL_MOTD)
			getAmotd().add(event.getResponse().split(":-")[1]);
		
		if (event.getCode() == ReplyConstants.RPL_ENDOFMOTD)
		{
			for (Iterator<String> it = getAmotd().iterator(); it.hasNext();)
			{
				MessageQueue.addQueue(MessageQueueEnum.IRC_MOTD,it.next() );
			}
			DebuggerQueue.addDebug(this, Level.INFO, "Receiving MOTD");
			setAmotd(new ArrayList<String>());
		}
		super.onServerResponse(event);
	}

}
